package com.palmshop.online.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.palmshop.online.dao.ReturnListDao;
import com.palmshop.online.entity.PalmResult;
import com.palmshop.online.entity.ReturnList;
import com.palmshop.online.entity.ReturnListDetail;
import com.palmshop.online.service.ReturnListService;
import com.palmshop.util.Qutil;

/**
 * 退货service的实现类
 * @author quan
 *
 */
@Service
@Transactional
public class ReturnListServiceImpl implements ReturnListService {

	@Resource
	private ReturnListDao dao;

	/**
	 * 添加一条退货信息
	 */
	public PalmResult addReturnList(String saleListUnique) {
		PalmResult result=new PalmResult();
		List<Map<String,Object>>list=dao.findSaleBySaleUnique(saleListUnique);
		if(list!=null&&list.size()>0){
			ReturnList returns=new ReturnList();
			String retListUnique = Qutil.createNum();
			returns.setRetListUnique(retListUnique);//退货唯一性标识
			returns.setShopUnique((String)list.get(0).get("shopUnique"));//店铺唯一性标识
			returns.setRetListDatetime(Qutil.getTime());//退货时间
			returns.setRetListTotal(Double.parseDouble(list.get(0).get("saleListTotal").toString()));//退款总额
			returns.setCusUnique((String)list.get(0).get("cusUnique"));//用户唯一性标识
			returns.setRetListState(1);//退款状态:1-未退款，2-已退款
			returns.setRetListHandlestate(1);//退货申请受理状态：1未处理，2-已受理，3受理完毕
			dao.addReturnList(returns);
			for(int i=0;i<list.size();i++){
				
				ReturnListDetail listDetail=new ReturnListDetail();
				String goodsUnique=(String)list.get(i).get("goodsUnique");//商品唯一性标识
				Integer retListDetailCount=(Integer)list.get(i).get("saleListDetailCount");//商品数量
				Double retListDetailPrice=(Double)list.get(i).get("saleListDetailPrice");//商品价格
				listDetail.setRetListUnique(retListUnique);
				listDetail.setGoodsUnique(goodsUnique);
				listDetail.setRetListDetailCount(retListDetailCount);
				listDetail.setRetListDetailPrice(retListDetailPrice);
				dao.addReturnListDetails(listDetail);
				
			}
			result.setStatus(1);
		}
		return result;
	}

	
	/**
	 * 根据retListUnique删除对应的退货信息
	 */
	public PalmResult deleteReturnList(String retListUnique) {
		PalmResult result=new PalmResult();
		if((dao.deleteReturnList(retListUnique)>0)&&(dao.deleteReturnListDetail(retListUnique)>0)){
			result.setStatus(1);
		}
		return result;
	}
	
	
	
	

}
